#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int sumn = 0;
        for (auto a : nums)
            sumn += a;
        //sort(nums.begin(),nums.end());
        int target = sumn - x;
        if (target == 0)
            return nums.size();
        else if (target < 0)
            return -1;
        int sum = 0;
        int ret = -1;
        for (int left = 0, right = 0; right < nums.size(); right++)
        {
            sum += nums[right];
            while (sum > target)
                sum -= nums[left++];
            if (sum == target)
                ret = max(ret, right - left + 1);
        }
        //cout<<right-left<<endl;
        //cout<<ret;
        if (ret != -1)return nums.size() - ret;
        else
            return ret;
    }
};